package logic

import (
	"context"
	"fmt"
	"market/model/redis"
	"math/rand"
	"time"

	"market/marketrpc/internal/svc"
	"market/marketrpc/marketrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *marketrpc.SendSmsRequest) (*marketrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	one := redis.Client.Get("sendSmsOne" + in.Phone)

	if one.Val() >= "1" {
		return nil, fmt.Errorf("一分钟内只能发送一次")
	}
	code := rand.Intn(9000) + 1000
	/*	sms, err := pkg.SendSms(in.Phone, strconv.Itoa(code))
		if err != nil {
			return nil, err
		}
		if *sms.Body.Code != "OK" {
			return nil, fmt.Errorf(*sms.Body.Message)
		}*/
	redis.Client.Set("sendSmsOne"+in.Phone, "1", time.Minute*1)
	redis.Client.Set("Sms"+in.Source+in.Phone, code, time.Minute*5)
	return &marketrpc.SendSmsResponse{}, nil
}
